﻿using System.Collections.Generic;
using System.Web;
using System.Net.Mail;
using System.Web.Configuration;
using System.Net.Configuration;

namespace AspNetInfo.Detectors
{
    public class EmailDetector
    {
        public static Dictionary<string, string> GetSmtpClientSettings()
        {
            var settings = new Dictionary<string, string>();
            var smtp = new SmtpClient();

            settings.Add("Host", smtp.Host);
            settings.Add("Port", smtp.Port.ToString());
            settings.Add("DeliveryMethod", smtp.DeliveryMethod.ToString());
            settings.Add("PickupDirectoryLocation", smtp.PickupDirectoryLocation);
            settings.Add("EnableSsl", smtp.EnableSsl.ToString());
            settings.Add("UseDefaultCredentials", smtp.UseDefaultCredentials.ToString());
            settings.Add("Timeout", smtp.Timeout.ToString());
            settings.Add("Username", "N/A");
            settings.Add("Password", "N/A");
            settings.Add("From", "N/A");

            return settings;
        }

        public static Dictionary<string, string> GetSmtpConfig()
        {
            var settings = new Dictionary<string, string>();
            var config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            var mailSettings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

            settings.Add("Host", mailSettings.Smtp.Network.Host);
            settings.Add("Port", mailSettings.Smtp.Network.Port.ToString());
            settings.Add("DeliveryMethod", mailSettings.Smtp.DeliveryMethod.ToString());
            settings.Add("PickupDirectoryLocation", mailSettings.Smtp.SpecifiedPickupDirectory.PickupDirectoryLocation);
            settings.Add("EnableSsl", "-");
            settings.Add("UseDefaultCredentials", mailSettings.Smtp.Network.DefaultCredentials.ToString());
            settings.Add("Timeout", "-");
            settings.Add("Username", mailSettings.Smtp.Network.UserName);
            settings.Add("Password", mailSettings.Smtp.Network.Password);
            settings.Add("From", mailSettings.Smtp.From);

            return settings;
        }

        public static void SendEmail(string from, string to, string subject, string message, bool isBodyHtml, SmtpClient smtp)
        {
            var testEmail = new MailMessage(from, to);
            testEmail.Subject = subject;
            testEmail.Body = message;
            testEmail.IsBodyHtml = isBodyHtml;

            smtp.Send(testEmail);
        }

        public static SmtpClient GetDefaultSmtp()
        {
            return new SmtpClient();
        }

        public static SmtpClient GetCustomSmtp(string host, int port,
            SmtpDeliveryMethod delivery, string pickupLocation, int timeout, bool enableSsl)
        {
            var smtp = GetDefaultSmtp();
            smtp.Host = host;
            smtp.Port = port;
            smtp.DeliveryMethod = delivery;
            smtp.PickupDirectoryLocation = pickupLocation;
            smtp.Timeout = timeout;
            smtp.EnableSsl = enableSsl;
            return smtp;
        }

        public static SmtpClient SetSmtpCredentials(SmtpClient smtp, string username, string password)
        {
            var smtpUser = new System.Net.NetworkCredential();
            smtpUser.UserName = username;
            smtpUser.Password = password;
            smtp.Credentials = smtpUser;

            return smtp;
        }
    }
}
